//
//  CustomReactionDetailViewController.m
//  autostainer
//
//  Created by lurenjia on 2017/6/18.
//  Copyright © 2017年 walker. All rights reserved.
//

#import "CustomReactionDetailViewController.h"
#import "Global.h"

@interface CustomReactionDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (copy, nonatomic) NSMutableString *reactionStr;
@property (assign, nonatomic) BOOL isFirst;
@property (strong,nonatomic) NSMutableArray *arrM;
@end

@implementation CustomReactionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:_navTitleStr];
    _arrM = [NSMutableArray array];
//    @[@"无水乙醇",@"水",@"BS液",@"橘黄G",@"伊红",@"HCL",@"EA50",@"DNA染液",@"苏木素",@"95%无水乙醇",@"75%无水乙醇",@"风干"];
    _reactionStr = [NSMutableString stringWithFormat:@"FE03010000"];
    [_arrM addObject:@"FE"];
    [_arrM addObject:@"03"];
    [_arrM addObject:@"01"];
    [_arrM addObject:@"00"];
    [_arrM addObject:@"00"];
    if ([_navTitleStr isEqualToString:@"用户自定义类型1"]) {
        _isFirst = YES;
    }
    else {
        _isFirst = NO;
    }
    [_reactionStr appendString:[self stringWithHexNumber:self.dataArray.count]];
    [self.dataArray enumerateObjectsUsingBlock:^(ExperimentModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        model.indexStr = [self stringWithHexNumber:idx+1];
        [self addString:_reactionStr model:model];
    }];
}

- (void)addString:(NSMutableString *)stringM model:(ExperimentModel *)model{
    NSString *nameString = model.name;
    PickResultModel *resultModel = model.timeModel;
    if (_isFirst) {
        [_reactionStr appendString:@"06"];
    }
    else {
        [_reactionStr appendString:@"07"];
    }

    [_reactionStr appendString:model.indexStr];
    NSString *str;
    if ([nameString isEqualToString:@"无水乙醇"]) {
        str = @"01";
    }
    else if ([nameString isEqualToString:@"水"]) {
        str = @"02";
    }
    else if ([nameString isEqualToString:@"BS液"]) {
        str = @"03";
    }
    else if ([nameString isEqualToString:@"橘黄G"]) {
        str = @"03";
    }
    else if ([nameString isEqualToString:@"伊红"]) {
        str = @"03";
    }
    else if ([nameString isEqualToString:@"HCL"]) {
        str = @"04";
    }
    else if ([nameString isEqualToString:@"EA50"]) {
        str = @"05";
    }
    else if ([nameString isEqualToString:@"DNA染液"]) {
        str = @"06";
    }
    else if ([nameString isEqualToString:@"苏木素"]) {
        str = @"06";
    }
    else if ([nameString isEqualToString:@"95%无水乙醇"]) {
        str = @"07";
    }
    else if ([nameString isEqualToString:@"75%无水乙醇"]) {
        str = @"08";
    }
    else if ([nameString isEqualToString:@"风干"]) {
        str = @"09";
    }
    
    [_reactionStr appendString:str];
    [_arrM addObject:str];
    [_reactionStr appendString:[self stringWithHexNumber:resultModel.allTime]];
    [_arrM addObject:[self stringWithHexNumber:resultModel.allTime]];
    
}

- (NSString *)stringWithHexNumber:(NSUInteger)hexNumber{
    
    char hexChar[6];
    sprintf(hexChar, "0%x", (int)hexNumber);
    
    NSString *hexString = [NSString stringWithCString:hexChar encoding:NSUTF8StringEncoding];
    
    return hexString;
}

- (NSString *)strWith16Number:(NSString *)str {
    NSString *strNumber = [NSString stringWithFormat:@"0x%@",str];
    NSString *cardId2 = [strNumber substringWithRange:NSMakeRange(2,2)];
    cardId2 = [NSString stringWithFormat:@"%ld",strtoul([cardId2 UTF8String],0,16)];
    return cardId2;
}

- (NSString *)checkOutSringWithArrM:(NSMutableArray<NSString *> *)arrM {
    
    NSInteger index = 0;
    for (int i=0; i<arrM.count; i++) {
        NSString *str = [self strWith16Number:arrM[i]];
        if (i == 0) {
            index = str.integerValue;
        }
        else {
            index = index ^ str.integerValue;
        }
    }
    NSString *string = [self stringWithHexNumber:index];
    return string;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)doneBtnClicked:(id)sender {
    NSString *str = [self checkOutSringWithArrM:_arrM];
    [_reactionStr appendString:str];
    [_reactionStr appendString:@"FF"];
    ReactionViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ReactionViewController"];
    vc.dataArray = self.dataArray;
    vc.ReactionStr = _reactionStr;
    [self.navigationController pushViewController:vc animated:true];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
        UILabel *nameLabel = [cell.contentView viewWithTag:1];
        UILabel *timeLabel = [cell.contentView viewWithTag:2];
        UILabel *noteLabel = [cell.contentView viewWithTag:3];
        
        ExperimentModel *model = self.dataArray[indexPath.row-1];
        nameLabel.text = model.name;
        if (model.time) {
            timeLabel.text = [NSString stringWithFormat:@"%@",model.time];
        }
        else {
            timeLabel.text = @"";
        }
        if (model.note) {
            noteLabel.text = [NSString stringWithFormat:@"%@℃",model.note];
        }else {
            noteLabel.text = @"";
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}


@end
